package cn.mrlong.base.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

/**
 * 执行ping 域名功能
 */
public class PingUtil {
    public interface CallBack {
        void result(boolean b);
    }

    public static void pingAsync(String str, final CallBack callBack) {

        Observable.just(str)
                .map(new Function<String, Boolean>() {
                    @Override
                    public Boolean apply(@NonNull String strUrl) throws Exception {
                        boolean resault = true;
                        Process p;
                        BufferedReader successReader = null;
                        BufferedReader errorReader = null;
                        try {
                            if (null!=(strUrl)) {
                                try {
                                    if (strUrl.contains("http://")) {
                                        strUrl = strUrl.replaceFirst("http://", "");
                                    }
                                    if (strUrl.contains("https://")) {
                                        strUrl = strUrl.replaceFirst("https://", "");
                                    }
                                    if (strUrl.contains("/emdc-app/")) {
                                        strUrl = strUrl.replace("/emdc-app/", "");
                                    }
                                    if (strUrl.contains(":18080")){
                                        strUrl = strUrl.replace(":18080", "");
                                    }
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }
                            //ping -c 3 -w 100  中  ，-c 是指ping的次数 3是指ping 3次 ，-w 100  以秒为单位指定超时间隔，是指超时时间为100秒
                            p = Runtime.getRuntime().exec("ping -c 3 -w 100 " + strUrl);
                            // success
                            successReader = new BufferedReader(new InputStreamReader(p.getInputStream()));
                            // error
                            errorReader = new BufferedReader(new InputStreamReader(p.getErrorStream()));
                            String lineStr;
                            while ((lineStr = successReader.readLine()) != null) {
                                // receive
                                System.out.println("==>"+lineStr + "\r\n");
                            }
                            while ((lineStr = errorReader.readLine()) != null) {
                                // receive
                                System.out.println("==>"+lineStr + "\r\n");
                            }
                            int status = p.waitFor();
                            if (status == 0) {
                                resault = true;
                            } else {
                                resault = false;
                            }
                        } catch (IOException e) {
                            e.printStackTrace();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        } catch (Exception e) {
                            e.printStackTrace();
                        } finally {
                            try {

                                if (successReader != null) {
                                    successReader.close();
                                }
                                if (errorReader != null) {
                                    errorReader.close();
                                }
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                        return resault;
                    }
                })
                .subscribeOn(Schedulers.io())//子线程查询数据
                .observeOn(AndroidSchedulers.mainThread())//主线程更新ui
                .subscribe(new Consumer<Boolean>() {
                    @Override
                    public void accept(Boolean aBoolean) throws Exception {
                        callBack.result(aBoolean);
                    }
                });
    }

}
